//
//  endTableViewController.m
//  CityTableViewOne
//
//  Created by 苗钧豪 on 16/7/26.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "endTableViewController.h"
#import "TableViewController.h"
#import "Qu.h"
#import "Sheng.h"
#import "Shi.h"
#import "endTableViewController.h"
@interface endTableViewController ()
@property(nonatomic,strong)NSMutableArray *Array;
@property(nonatomic,strong)NSMutableArray *Array2;
@property(nonatomic,strong)NSMutableArray *Array3;
@property(nonatomic,strong)NSMutableArray *arr;
@property(nonatomic,strong)Sheng *sheng;
@property(nonatomic,strong)Shi *shi;
@property(nonatomic,strong)Qu *qu;

@end

@implementation endTableViewController
- (void)getData {
    NSString *filepath = [[NSBundle mainBundle]pathForResource:@"area" ofType:@"txt"];
    NSString *string = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:nil];
    self.Array = [NSMutableArray array];
    self.Array2 = [NSMutableArray array];
    self.Array3 = [NSMutableArray array];
    _sheng.array = [NSMutableArray array];
    _shi.array = [NSMutableArray array];
    self.arr = [NSMutableArray array];
    // 根据回车分割字符串
    NSArray *array = [string componentsSeparatedByString:@"\n"];
    for (NSString *area in array) {
        if (![area hasPrefix:@" "]) {
            NSArray *arr = [area componentsSeparatedByString:@" "];
            Sheng *sheng = [[Sheng alloc]init];
            sheng.name = arr.firstObject;
            sheng.number = arr.lastObject;
            [_Array addObject:sheng];
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            NSString *str = [area substringFromIndex:2];
            NSArray *arr = [str componentsSeparatedByString:@" "];
            Sheng *sheng = _Array.lastObject;
            Shi *shi = [[Shi alloc]init];
            shi.name = arr.firstObject;
            shi.number = arr.lastObject;
            [sheng.array addObject:shi];
            //                        NSLog(@"%@", sheng.array);
        } else if ([area hasPrefix:@"    "]) {
            NSString *string = [area substringFromIndex:4];
            NSArray *arr = [string componentsSeparatedByString:@" "];
            Sheng *sheng = _Array.lastObject;
            Shi *shi = sheng.array.lastObject;
            Qu *qu = [[Qu alloc]init];
            qu.name = arr.firstObject;
            qu.number = arr.lastObject;
            [shi.array addObject:qu];
        }
    }
    
    //    for (NSDictionary *dic in self.Array) {
    //        self.arr = dic[@"array"];
    //    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"MoMo"];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

//    Sheng *sheng = _Array[section];
//    Shi *shi = sheng.array[section];
//    return shi.array.count;
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"MoMo"];
    Sheng *sheng = _Array[indexPath.row];
    //    NSLog(@"%@",_sheng.array);
    
    Shi *shi = sheng.array[indexPath.row];
    Qu *qu = shi.array[indexPath.row];
    cell.textLabel.text = qu.name;
    cell.detailTextLabel.text = qu.number;
    // Configure the cell...
    
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
